package com.huike.clues.service.impl;

import com.huike.clues.mapper.SysDictMapper;
import com.huike.clues.service.ISysDictService;
import com.huike.common.config.BaseContext;
import com.huike.common.constant.Constants;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.domain.entity.SysDictType;
import com.huike.common.core.redis.RedisCache;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class ISysDictServiceImpl implements ISysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    @Resource
    private RedisCache redisCache;



    @PostConstruct
    public void init(){
        List<SysDictData> dataList = sysDictMapper.dataList(new SysDictData());
        for (SysDictData dictData : dataList) {
            redisCache.setCacheObject(getCacheKey(dictData.getDictType()),getCacheKey(dictData.getDictValue()));
        }
    }

    /**
     * 设置cache key
     *
     * @param
     * @return 缓存键key
     */
    private String getCacheKey(String sysdictKey)
    {
        return Constants.SYS_DICT_KEY + sysdictKey;
    }

    @Override
    public void dictDataInsert(SysDictData sysDictData) {
        SysDictData dictData = new SysDictData();
        BeanUtils.copyBeanProp(dictData,sysDictData);
        dictData.setCreateBy(BaseContext.getCurrentUser().getUserName());
        dictData.setCreateTime(new Date());
        sysDictMapper.dictDataInsert(dictData);
    }


    @Override
    public void clearCache() {
        Collection<String> keys = redisCache.keys(Constants.SYS_DICT_KEY + "*");
        redisCache.deleteObject(keys);
    }

    @Override
    public void dataDelete(List<String> dictCodes) {
        sysDictMapper.dataDelete(dictCodes);
    }

    @Override
    public SysDictData dataSelectBydictCode(Integer dictCode) {

        return sysDictMapper.dataSelectBydictCode(dictCode);
    }

    @Override
    public List<SysDictData> dataList(SysDictData sysDictData) {

        return sysDictMapper.dataList(sysDictData);
    }

    @Override
    public void dataUpdate(SysDictData sysDictData) {
        SysDictData dictData = new SysDictData();
        BeanUtils.copyBeanProp(dictData,sysDictData);
        dictData.setUpdateBy(BaseContext.getCurrentUser().getUserName());
        dictData.setUpdateTime(new Date());
        sysDictMapper.dataUpdate(dictData);
    }

    @Override
    public void typeDelete(List<Integer> dictIds) {
        for (Integer dictId : dictIds) {
            SysDictType sysDictType = sysDictMapper.typeSelect(dictId);
            List<SysDictData> dataList = sysDictMapper.dataTypeselect(sysDictType.getDictType());
            if (dataList !=null && dataList.size()>0){
                throw new CustomException("不能进行删除操作");
            }
        }

        sysDictMapper.typeDelete(dictIds);
    }

    @Override
    public List<SysDictType> optionselect() {

        return sysDictMapper.optionselect();
    }

    @Override
    public void dictTypeUpdate(SysDictType sysDictType) {
        SysDictType dictType = new SysDictType();
        BeanUtils.copyBeanProp(dictType,sysDictType);
        dictType.setUpdateTime(new Date());
        dictType.setUpdateBy(BaseContext.getCurrentUser().getUserName());
        sysDictMapper.dictTypeUpdate(dictType);
    }

    @Override
    public SysDictType typeSelect(Integer dictId) {
        return sysDictMapper.typeSelect(dictId);
    }

    @Override
    public List<SysDictData> dataTypeselect(String dictType) {

        return sysDictMapper.dataTypeselect(dictType);
    }

    /**
     * 新增字典类型
     * @param
     * @return
     */
    @Override
    public void dictTypeInsert(SysDictType sysDictType) {
        SysDictType dictType = new SysDictType();
        BeanUtils.copyBeanProp(dictType,sysDictType);
        dictType.setCreateBy(BaseContext.getCurrentUser().getUserName());
        dictType.setCreateTime(new Date());

        sysDictMapper.dictTypeInsert(dictType);
    }

    /**
     * 分页查询数据字典类型信息
     * @param dictType
     * @return
     */

    @Override
    public List<SysDictType> list(SysDictType dictType) {

        return sysDictMapper.selectlist(dictType);
    }
}
